﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Iguana.Core.HypterText
{
    internal static class UtilElementWriter
    {

        internal static string WriteClosing(bool selfClosing, string elementStart)
        {
            if (selfClosing)
            {
                return SR.SR_ELEMENT_SELF_CLOSING;
            }
            else
            {
                return String.Format(SR.SR_ELEMENT_CLOSING, elementStart);
            }
        }

        internal static string WriteStartWithAttributes(string start, AttributeCollection attributes, bool includeAll)
        {
            StringBuilder m_Builder = new StringBuilder();
            m_Builder.Append(String.Format(SR.SR_ELEMENT_START_F, start));

            foreach (var i in attributes)
            {
                if (includeAll)
                {
                    m_Builder.Append(String.Format(SR.SR_ATTRIBUTE_NAME_VALUE_PAIR, i.Name, i.Value));
                }
                else
                {
                    if (i.Required)
                    {
                        m_Builder.Append(String.Format(SR.SR_ATTRIBUTE_NAME_VALUE_PAIR, i.Name, i.Value));
                    }
                }
            }

            string m_Temp = m_Builder.ToString();
            m_Temp = m_Temp.TrimEnd(new char[] { ' ' });
            m_Temp = m_Temp + SR.SR_ELEMENT_END;
            return m_Temp;
        }

        internal static string WriteWholeElement(IElement element, bool includeAll)
        {
            StringBuilder m_Builder = new StringBuilder();
            m_Builder.Append(WriteStartWithAttributes(element.TagStart,element.Attributes,includeAll));

            if (element.Type == ElementType.Normal)
            {
                INormalElement m_Normal = (INormalElement)element;
                m_Builder.Append(m_Normal.Content);
            }

            m_Builder.Append(WriteClosing(false, element.TagStart));

            return m_Builder.ToString();
        }
    }
}
